package com.sllp.C71S3TsllpShop.web;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttribute;

import com.github.pagehelper.PageHelper;
import com.sllp.C71S3TsllpShop.bean.BigType;
import com.sllp.C71S3TsllpShop.bean.Car;
import com.sllp.C71S3TsllpShop.bean.Favorite;
import com.sllp.C71S3TsllpShop.bean.Result;
import com.sllp.C71S3TsllpShop.bean.User;
import com.sllp.C71S3TsllpShop.biz.impl.CarBizImpl;
import com.sllp.C71S3TsllpShop.biz.impl.FavoriteBizImpl;
import com.sllp.C71S3TsllpShop.dao.FavoriteMapper;

@Controller
public class FavoriteAction {

	@Resource
	private FavoriteBizImpl fbiz;
	
	@Resource
	private CarBizImpl cbiz;
	
	@Resource
	private FavoriteMapper fm;
	
	@GetMapping("favorite")
	public String toFavorite(HttpSession session,@SessionAttribute("btlist") List<List<BigType>> lists,
			Model m,String pre,String next,
			@SessionAttribute("loginUser") User user,
			@SessionAttribute(name="page",required=false)Integer page) {
		m.addAttribute("btlist", lists);
		if(next==null){
			page=1;
		}else if(Integer.valueOf(next)>0){
			page=Integer.valueOf(next);
		}
		if(pre!=null){
			page=1;
		}
		PageHelper.startPage(page,7);
		//从新设置分页的页码，相当于page++
	    m.addAttribute("page", page);
	    m.addAttribute("pag", page-1);
		//收藏夹显示
		List<Favorite> flist=fbiz.findfavorite(user);
		m.addAttribute("flist", flist);
		
		//右上角购物车显示
//		List<Car> clist=cbiz.findCar();
//		m.addAttribute("clist", clist);
		
		return "Member_collect";
	}
	
	/**
	 * 删除收藏的商品
	 */
	@ResponseBody
	@PostMapping("del")
	public Result del(Integer id){
		int ok=fm.deleteByPrimaryKey(id);
		if(ok>0) {
			return new Result(ok,"删除成功");
		}
		return new Result(ok, "删除失败"); 
	}
	
	@ModelAttribute
	public void searchCar(@SessionAttribute("loginUser") User user,Model m) {
		if(user != null) {
			List<Car> clist=cbiz.findCar(user);
			m.addAttribute("clist", clist);
		}
	}
}
